package ro.gateway.aida.obj;

import java.util.ArrayList;
import java.util.Vector;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: OrganizationInv.java,v 1.1 2004/10/24 23:37:10 mihaipostelnicu Exp $
 */
public class OrganizationInv extends AIDAActivityObject
{
      private long org_id;
      private Organization org;
      public OrganizationRole role;

      public OrganizationInv( Organization org, Integer local_role_id )
      {
            this.role = OrganizationRole.getCachedByLocalId( local_role_id );
            if ( this.role == null )
                  throw new RuntimeException( "Unsupported role :D" );

            setOrg( org );
            this.objectType = TYPE_ORGANIZATION_INVOLVED;
      }

      public OrganizationInv()
      {
            this.objectType = TYPE_ORGANIZATION_INVOLVED;
      }

      public OrganizationInv( Organization org, OrganizationRole role )
      {
            this.role = role;
            this.setOrg( org );
            this.objectType = TYPE_ORGANIZATION_INVOLVED;
      }

      public Organization getOrg()
      {
            return org;
      }

      public long getOrg_id()
      {
            return org_id;
      }

      public void setOrg_id( long org_id )
      {
            this.org_id = org_id;
      }

      public void setOrg( Organization org )
      {
            this.org = org;
            if ( org != null )
            {
                  this.org_id = org.getId();
            }
            else
            {
                  this.org_id = -1;
            }
      }

      /**
       * Primeste un Array de obiecte OrganizationInv si returneaza
       * un vector de Organization
       * @return
       */
      public static Vector buildOrgInvVector( OrganizationInv[] items )
      {
            Vector result = new Vector();
            if ( items == null )
            {
                  return result;
            }

            for ( int i = 0; i < items.length; i++ )
            {
                  result.add( items[ i ].getOrg() );
            }

            return result;
      }

      /**
       * Primeste un Vector de obiecte Organization si returneaza
       * un array de OrganizationInv
       * @param orgs
       * @return
       */
      public static OrganizationInv[] buildOrgInvArray( ArrayList orgs, Integer local_role_id )
      {
            OrganizationInv result[] = null;

            if ( orgs == null ) return result;
            OrganizationRole role = OrganizationRole.
                  getCachedByLocalId( local_role_id );
            if ( role == null )
            {
                  System.err.println( "buildOrgInvArray: can't find role:" + local_role_id );
                  return result;
            }

            ArrayList items = new ArrayList();
            for ( int i = 0; i < orgs.size(); i++ )
            {
                  Object o =  orgs.get( i );
                  if ( o instanceof Organization )
                  {
                        OrganizationInv org = new OrganizationInv( ( Organization ) o, role );
                        items.add( org );
                  }
            }

            if ( items.size() > 0 )
            {
                  result = new OrganizationInv[ items.size() ];
                  items.toArray( result );
            }

            return result;
      }
	/**
	 * @return Returns the role.
	 */
	public OrganizationRole getRole() {
		return role;
	}
	/**
	 * @param role The role to set.
	 */
	public void setRole(OrganizationRole role) {
		this.role = role;
	}
}
